package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.bean.House;
import com.hq.bean.User;
import com.hq.service.HouseService;
import com.hq.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HouseController {
    @Resource
    private HouseService houseService;
    @RequestMapping("list.do")
    public String list(Model model, @RequestParam(value = "cpage",defaultValue = "1")Integer cpage,
                       String mohu1,
                       String startTime,String endTime,
                       String startTime1,String endTime1,
                       String startTime2,String endTime2,
                       String mohu2,
                       String mohu3,
                       String mohu4,
                       String mohu5,
                       String mohu6){
        Map map = new HashMap();
        map.put("mohu1",mohu1);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("startTime1",startTime1);
        map.put("endTime1",endTime1);
        map.put("startTime2",startTime2);
        map.put("endTime2",endTime2);
        map.put("mohu2",mohu2);
        map.put("mohu3",mohu3);
        map.put("mohu4",mohu4);
        map.put("mohu5",mohu5);
        map.put("mohu6",mohu6);
        PageHelper.startPage(cpage,3);
        List list = houseService.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(House house, @RequestParam("photo")MultipartFile photo,Integer[] mids,HttpSession session){
        if (photo.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            house.setFujian(upload);
        }
        int i = houseService.add(house, mids);
        return i;
    }
    @RequestMapping("toadd.do")
    public String toadd(){
        return "add";
    }

    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer hid){
       return houseService.del(hid);
    }

    @RequestMapping("login.do")
    public String login(User user){
        User user1 = houseService.login(user);
        if (user1!=null){
            return "redirect:list.do";
        }else {
            return "redirect:index.jsp";
        }
    }
}
